package com.efast.cafe.portal.controller.api;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.api.PortalApiModuleBean;
import com.efast.cafe.portal.bean.api.PortalApiProjectBean;
import com.efast.cafe.portal.bean.api.PortalApiProjectDto;
import com.efast.cafe.portal.service.api.IPortalApiModuleService;
import com.efast.cafe.portal.service.api.IPortalApiProjectService;
import com.efast.cafe.portal.service.api.IPortalApiSystemService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * <p>Controller</p>
 * <p>Table: portal_api_module - 所属模块定义</p>
 *
 * @since 2017-08-10 02:38:08
 */
@Controller
@RequestMapping("/frame_center/api")
public class PortalApiModuleController extends AbstractController{

	@Autowired
	private IPortalApiModuleService portalApiModuleService;
	
	@Autowired
	private IPortalApiSystemService portalApiSystemService;
	
	@Autowired
	private IPortalApiProjectService portalApiProjectService;
	
	

	@RequestMapping(value = "/module/add")
	public @ResponseBody ResultBean saveApiModuledefine(
			@RequestBody  PortalApiModuleBean portalApiModuleBean,
			HttpServletRequest request){
		//RequestContext context=new RequestContext(request);
		/*ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));*/
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		Integer id = null;
		try{
			id = portalApiModuleService.insertApiModule(portalApiModuleBean);
			result.setObject(id.toString());
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage());
			}else{
				//result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}
	
	/**
	 * 修改模块
	 * @Date        :      2017年8月11日
	 * @param portalApiModuleBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/module/update")
	public @ResponseBody ResultBean updateApiModule(
			@RequestBody  PortalApiModuleBean portalApiModuleBean,
			HttpServletRequest request){
		//RequestContext context=new RequestContext(request);
		/*ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));*/
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try{
//			portalApiModuleService.updateApiModule(portalApiModuleBean);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage());
			}else{
				//result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}
	
	/**
	 * 删除模块
	 * @Date        :      2017年8月14日
	 * @param portalApiModuleBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/module/delete")
	public @ResponseBody ResultBean deleteApiModule(
			@RequestBody  PortalApiModuleBean portalApiModuleBean,
			HttpServletRequest request){
		//RequestContext context=new RequestContext(request);
		/*ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));*/
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try{
			portalApiModuleService.deleteApiModule(portalApiModuleBean);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage());
			}else{
				//result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 模块发布
	 * @Date        :      2017年8月15日
	 * @param portalApiModuleBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/module/publish")
	public @ResponseBody ResultBean publishApiModule(
			@RequestBody  PortalApiModuleBean portalApiModuleBean,
			HttpServletRequest request){
		//RequestContext context=new RequestContext(request);
		/*ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));*/
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try{
			portalApiModuleBean.setStatus((byte)1);
			portalApiModuleService.publishApiModule(portalApiModuleBean);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage());
			}else{
				//result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}
	
	/**
	 * 模块升级
	 * @Date        :      2017年8月15日
	 * @param portalApiModuleBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/module/upgrade")
	public @ResponseBody ResultBean upgradeApiModule(
			@RequestBody  PortalApiModuleBean portalApiModuleBean,
			HttpServletRequest request){
		//RequestContext context=new RequestContext(request);
		/*ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));*/
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try{
			portalApiModuleBean.setStatus((byte)2);
			portalApiModuleService.upgradeApiModule(portalApiModuleBean);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage());
			}else{
				//result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}
	/**
	 * 
	 * @Date        :      2017年8月24日
	 * @param portalApiModuleBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/module/queryAllVersion")
	public @ResponseBody ResultBean queryModuleBySystemIdAndUUid(
			@RequestBody  PortalApiProjectDto apiProjectDto,
			HttpServletRequest request){
		
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			if(apiProjectDto.getType()!=null && apiProjectDto.getType().intValue()==CommonConstant.PortalNodeType.PROJECR.intValue()){
				PortalApiProjectBean apiProjectBean = new PortalApiProjectBean();
				apiProjectBean.setUuid(apiProjectDto.getUuid());
				apiProjectBean.setId(apiProjectDto.getId());
				List<PortalApiProjectBean>  listProject = portalApiProjectService.queryProjectByIdAndUUid(apiProjectBean);
				result.setObject(listProject);
			}else if(apiProjectDto.getType()!=null && apiProjectDto.getType().intValue()==CommonConstant.PortalNodeType.SYSTEM.intValue()){
				result.setObject(portalApiSystemService.queryApiSystemOtherVersions(apiProjectDto.getPid(),
						apiProjectDto.getId(), apiProjectDto.getStatus()));

			}else if (apiProjectDto.getType()!=null && apiProjectDto.getType().intValue()==CommonConstant.PortalNodeType.MODULE.intValue()) {
				PortalApiModuleBean portalApiModuleBean = new PortalApiModuleBean();
				portalApiModuleBean.setUuid(apiProjectDto.getUuid());
				if(apiProjectDto.getStatus()!=null){
					portalApiModuleBean.setStatus(apiProjectDto.getStatus().byteValue());
				}
				portalApiModuleBean.setModuleId(apiProjectDto.getId());
				portalApiModuleBean.setSystemId(apiProjectDto.getPid());
				result.setObject(portalApiModuleService.queryModuleBySystemIdAndUUid(portalApiModuleBean));
			}
		} catch (Exception e) {
			result.setSuccess(false);		
			e.printStackTrace();
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage());
			}else{
				//result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}
	
	/**
	 * 查询系统下面所有的最新模块
	 * @Date        :      2017年8月24日
	 * @param portalApiModuleBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/module/queryModuleBySystemId")
	public @ResponseBody ResultBean queryModuleBySystemId(
			@RequestBody  PortalApiProjectDto portalApiProjectDto,
			HttpServletRequest request){
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			result.setObject(portalApiModuleService.queryModuleBySystemId(portalApiProjectDto));
		} catch (Exception e) {
			result.setSuccess(false);	
			e.printStackTrace();
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage());
			}else{
				//result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}
}